package com.backend.modules.task.controller;

import com.backend.common.model.R;
import com.backend.modules.task.convert.TaskSubmissionConvert;
import com.backend.modules.task.dto.StudentTaskQueryDTO;
import com.backend.modules.task.dto.TaskSubmissionDTO;
import com.backend.modules.task.entity.TaskSubmission;
import com.backend.modules.task.enums.TaskStatus;
import com.backend.modules.task.service.StudentTaskService;
import com.backend.modules.task.vo.TaskSubmissionVO;
import com.backend.modules.task.vo.TaskVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "学生任务", description = "学生任务相关接口")
@RestController
@RequestMapping("/v1/student/tasks")
@RequiredArgsConstructor
@PreAuthorize("hasRole('STUDENT')")
public class StudentTaskController {

    private final StudentTaskService studentTaskService;

    @Operation(summary = "分页查询任务")
    @GetMapping("/page")
    public R<Page<TaskVO>> page(
            @Parameter(description = "任务名称") @RequestParam(required = false) String name,
            @Parameter(description = "提交状态") @RequestParam(required = false) Boolean submitted,
            @Parameter(description = "截止状态") @RequestParam(required = false) TaskStatus status,
            @Parameter(description = "开始时间") 
            @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @Parameter(description = "结束时间") 
            @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @ParameterObject Pageable pageable) {
        
        StudentTaskQueryDTO query = new StudentTaskQueryDTO();
        query.setName(name);
        query.setSubmitted(submitted);
        query.setStatus(status);
        query.setStartTime(startTime);
        query.setEndTime(endTime);
        
        return R.ok(studentTaskService.page(query, pageable));
    }

    @Operation(summary = "获取任务详情")
    @GetMapping("/{taskId}")
    public R<TaskVO> getTaskDetail(@PathVariable Long taskId) {
        return R.ok(studentTaskService.getTaskDetail(taskId));
    }

    @Operation(summary = "提交任务")
    @PostMapping("/{taskId}/submit")
    public R<Void> submit(
            @PathVariable Long taskId,
            @Validated @RequestBody TaskSubmissionDTO dto) {
        studentTaskService.submit(taskId, dto);
        return R.ok();
    }

    @Operation(summary = "修改提交")
    @PutMapping("/{taskId}/submission")
    public R<Void> updateSubmission(
        @PathVariable Long taskId,
        @Validated @RequestBody TaskSubmissionDTO dto) {
        studentTaskService.updateSubmission(taskId, dto); 
        return R.ok();
    }

    @Operation(summary = "获取提交详情")
    @GetMapping("/submissions/{submissionId}")
    public R<TaskSubmissionVO> getSubmissionDetail(@PathVariable Long submissionId) {
        TaskSubmission submission = studentTaskService.getSubmissionById(submissionId);
        return R.ok(TaskSubmissionConvert.convert(submission));
    }

}